package cn.itcast.documentapis;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.UpdateByQueryAction;
import org.elasticsearch.index.reindex.UpdateByQueryRequestBuilder;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType;
import org.junit.jupiter.api.Test;

import java.net.InetAddress;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * @Description TODO : updateByQuery的最简单用法是更新索引中的每个文档而不更改源。 此用法可以启用新属性或其他在线映射更改。
 * @Author: zj
 * @Date: 15:05 2019/3/8
 * @Version 1.0
 **/
public class UpdateByQueryApi {

    @Test
    public void updateByquery() {
        TransportClient client = esClient.getClient();

        UpdateByQueryRequestBuilder updateByQueryRequestBuilder = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
        updateByQueryRequestBuilder.source("twitter")
                .filter(QueryBuilders.termQuery("user", "kimchy"))
                .size(100)
                .script(new Script(ScriptType.INLINE,
                        "ctx._source.awesome = 'absolutely'",
                        "painless",
                        new HashMap<String, Object>()));
    }


    public static void main(String[] args) throws Exception {
        String s = formatTime(new Date(), "yyyyMMdd HH:mm:ss");
        System.out.println(s);
    }
    private static final String yyyyMMddHHmmssSSS2 = "yyyy-MM-dd HH:mm:ss SSS";
    private static final String yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS";
    public static final String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    private static final String yyyyMMddHHmmss2 = "yyyyMMddHHmmss";
    private static final String YYYY_MM_DD = "yyyy-MM-dd";
    private static final String YYYY = "yyyy";
    private static final String MM = "MM";
    private static final String DD = "dd";

    public static String formatDateString3(String dateStr) throws Exception {
        Date date = toTime(dateStr,  yyyyMMddHHmmss);
        return formatTime(date,  yyyyMMddHHmmss2);
    }
    //字符串转换为日期
    public static Date toTime(String time, String pattern){
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            return format.parse(time);
        } catch (ParseException e) {

        }
        return null;
    }

    //日期转换为字符串
    public static String formatTime(Date date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }


}
